package com.aktk.battlenole;

import com.aktk.battlenole.GameData.Stages;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class SwitchActivity extends Activity {
	TextView lMessage;
	Button bSwitch;
	GameData game;
	String message;
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_switch);
        game = new GameData();
        
        /*if( game.numDevices == 2 )
        {
        	game.stage = Stages.GAME;
        	game.startGame();
        	//Intent i = new Intent(SwitchActivity.this, GameActivity.class);
    		//startActivity (i);
        }*/
        
        lMessage = (TextView)this.findViewById(R.id.messageLabelSwitch);
        bSwitch = (Button)this.findViewById(R.id.buttonSwitch);
        bSwitch.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                buttonPressed();
            }
            
        });
        
        setMessage();
    }
	
	//, , , 
	
	public void setMessage() {
		switch (game.stage) {
        case MAIN_PAGE : message = new String("Please hand device to Player 1 for ship setup.");
                 		 break;
        case PLAYER1_SETUP:  message = new String("Please hand device to Player 2 for ship setup.");
                 break;
        case PLAYER2_SETUP: game.startGame();
        case GAME: if( game.isGameOver() )
        		   {
        			   message = new String(game.winner() + " Won!  Press Done to start new game.");
        		   }
        		   else
        		   {
        			   game.nextTurn();
        			   if( game.playerTurn == 1 )
        			   {
        				   message = new String("Please hand device to Player 1 for turn.");
        			   }
        			   else
        			   {
        				   message = new String("Please hand device to Player 2 for turn.");
        			   }
        		   }
                 break;
        default: message = new String("ERROR: Please restart App.");
                 break;
		}
		
		lMessage.setText(message);
    }
    
    public void buttonPressed() {
    	Intent i;
    	
    	switch (game.stage) {
        case MAIN_PAGE : i = new Intent(SwitchActivity.this, SetupGameBoardActivity.class);
        				 game.stage = Stages.PLAYER1_SETUP;
                 		 break;
        case PLAYER1_SETUP: i = new Intent(SwitchActivity.this, SetupGameBoardActivity.class);
        				    game.stage = Stages.PLAYER2_SETUP;
        				    break;
        case PLAYER2_SETUP:  i = new Intent(SwitchActivity.this, GameActivity.class);
        					 game.stage = Stages.GAME;
        					 break;
        case GAME: if( game.isGameOver() && game.stage == Stages.GAME )
    			   {
    					i = new Intent(SwitchActivity.this, MainActivity.class);
    					startActivity (i);
    			   }
        		   else
        		   {
        			   i = new Intent(SwitchActivity.this, GameActivity.class);
                       break;
        		   }
        default: i = new Intent(SwitchActivity.this, MainActivity.class);
                 break;
		}
		startActivity (i);
    }

}
